package com.neptune.imapp.ui.compose.screen.mainPage.page

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.neptune.imapp.repository.vo.ApplyForMain
import com.neptune.imapp.ui.compose.screen.frontPage.login.autoCloseKeyboard
import com.neptune.imapp.ui.compose.screen.mainPage.MainViewModel
import kotlinx.coroutines.launch

@OptIn(
    ExperimentalComposeUiApi::class,
    ExperimentalMaterial3Api::class
)
@Composable
fun Search(viewModel: MainViewModel) {
    val keyController = LocalSoftwareKeyboardController.current
    val focusController = LocalFocusManager.current
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    val min = remember {
        mutableStateOf("")
    }
    val max = remember {
        mutableStateOf("")
    }
    val address = remember {
        mutableStateOf("")
    }
    val time = remember {
        mutableStateOf("")
    }
    val vacation = remember {
        mutableStateOf("")
    }
    val education = remember {
        mutableStateOf("")
    }
    ModalNavigationDrawer(drawerContent = {
        Column(
            modifier = Modifier
                .padding(10.dp)
                .weight(1.0f),
            verticalArrangement = Arrangement.spacedBy(5.dp)
        ) {
            Text(text = "预期薪水", fontSize = 20.sp, color = MaterialTheme.colorScheme.primary)
            Row(
                horizontalArrangement = Arrangement.spacedBy(5.dp),
                modifier = Modifier
                    .padding(5.dp)
                    .wrapContentSize(Alignment.Center)
            ) {
                OutlinedTextField(
                    value = min.value,
                    onValueChange = { min.value = it },
                    placeholder = {
                        Text(
                            text = "最低薪水(K)"
                        )
                    }, modifier = Modifier.weight(1.0f),
                    shape = RoundedCornerShape(20.dp)
                )
                Text(
                    text = "──",
                    modifier = Modifier
                        .height(50.dp)
                        .wrapContentSize(Alignment.Center)
                )
                OutlinedTextField(
                    value = max.value,
                    onValueChange = { max.value = it },
                    placeholder = {
                        Text(
                            text = "最高薪水(K)"
                        )
                    }, modifier = Modifier.weight(1.0f),
                    shape = RoundedCornerShape(20.dp)
                )
            }
            Divider()
            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                Column(
                    modifier = Modifier
                        .weight(1.0f)
                ) {
                    Text(
                        text = "预期工作地点",
                        fontSize = 20.sp,
                        color = MaterialTheme.colorScheme.primary
                    )
                    OutlinedTextField(
                        value = address.value,
                        onValueChange = { address.value = it },
                        placeholder = {
                            Text(
                                text = "请输入地点(市)"
                            )
                        },
                        shape = RoundedCornerShape(20.dp)
                    )
                }
                Column(
                    modifier = Modifier
                        .weight(1.0f)
                ) {
                    Text(
                        text = "预期学历",
                        fontSize = 20.sp,
                        color = MaterialTheme.colorScheme.primary,
                        modifier = Modifier.wrapContentWidth(Alignment.CenterHorizontally)
                    )
                    OutlinedTextField(
                        value = education.value,
                        onValueChange = { education.value = it },
                        placeholder = {
                            Text(
                                text = "请输入学历"
                            )
                        },
                        shape = RoundedCornerShape(20.dp)
                    )
                }
            }
            Divider()
            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                Column(
                    modifier = Modifier
                        .weight(1.0f)
                ) {
                    Text(
                        text = "预期工作年龄",
                        fontSize = 20.sp,
                        color = MaterialTheme.colorScheme.primary
                    )
                    OutlinedTextField(
                        value = time.value,
                        onValueChange = { time.value = it },
                        placeholder = {
                            Text(
                                text = "请输入年数"
                            )
                        },
                        shape = RoundedCornerShape(20.dp)
                    )
                }
                Column(
                    modifier = Modifier
                        .weight(1.0f)
                ) {
                    Text(
                        text = "行业",
                        fontSize = 20.sp,
                        color = MaterialTheme.colorScheme.primary,
                        modifier = Modifier.wrapContentWidth(Alignment.CenterHorizontally)
                    )
                    OutlinedTextField(
                        value = vacation.value,
                        onValueChange = { vacation.value = it },
                        placeholder = {
                            Text(
                                text = "请输入行业名称"
                            )
                        },
                        shape = RoundedCornerShape(20.dp)
                    )
                }
            }
        }
        Row(
            horizontalArrangement = Arrangement.spacedBy(50.dp),
            modifier = Modifier.padding(horizontal = 20.dp)
        ) {
            Button(onClick = {
                scope.launch {
                    drawerState.close()
                    viewModel.applyFilter(
                        minSalary = if (min.value.isEmpty()) 0f else min.value.toFloat(),
                        maxSalary = if (max.value.isEmpty()) 0f else max.value.toFloat(),
                        address = address.value,
                        education = education.value,
                        time = if (time.value.isEmpty()) 0 else time.value.toInt(),
                        vacation = vacation.value,
                        name = viewModel.searchInfo.value
                    )
                }
            }, shape = RoundedCornerShape(5.dp), modifier = Modifier.weight(1.0f)) {
                Text(
                    text = "确定",
                    modifier = Modifier
                        .wrapContentWidth(Alignment.CenterHorizontally)
                )
            }
            Button(onClick = {
                scope.launch {
                    drawerState.close()
                }
            }, shape = RoundedCornerShape(5.dp), modifier = Modifier.weight(1.0f)) {
                Text(
                    text = "取消",
                    modifier = Modifier
                        .wrapContentWidth(Alignment.CenterHorizontally)
                )
            }
        }
    }, drawerState = drawerState, modifier = Modifier.padding(bottom = 80.dp)) {
        Column(
            verticalArrangement = Arrangement.spacedBy(10.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .autoCloseKeyboard(keyController, focusController)
                .padding(start = 20.dp, end = 20.dp, top = 10.dp)
        ) {
            val searchInfo = remember {
                mutableStateOf("")
            }
            OutlinedTextField(
                value = searchInfo.value,
                onValueChange = { searchInfo.value = it }, placeholder = {
                    Text(text = "搜索职位/公司")
                }, modifier = Modifier
                    .fillMaxWidth(),
                trailingIcon = {
                    Button(
                        onClick = {

                        },
                        shape = RoundedCornerShape(10.dp),
                        modifier = Modifier.padding(end = 5.dp)
                    ) {
                        Text(text = "搜索", fontSize = 18.sp, letterSpacing = 5.sp)
                    }
                }, leadingIcon = {
                    Text(text = "筛选▼", textAlign = TextAlign.Center, modifier = Modifier.clickable {
                        scope.launch {
                            drawerState.open()
                        }
                    })
                }, singleLine = true
            )
            val state = viewModel.applies.collectAsState().value
            LazyColumn(
                content = {
                    when (state) {
                        MainViewModel.ApplyListState.Loading -> {
                            item {
                                CircularProgressIndicator()
                            }
                        }
                        is MainViewModel.ApplyListState.Data -> {
                            items(state.apply.size) {
                                ApplyItem(apply = state.apply[it])
                            }
                        }
                    }
                }, contentPadding = PaddingValues(vertical = 10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp)
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ApplyItem(apply: ApplyForMain) {
    ElevatedCard(modifier = Modifier.fillMaxWidth()) {
        Column(
            modifier = Modifier.padding(10.dp),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.spacedBy(5.dp)
        ) {
            Row {
                Text(
                    text = apply.name,
                    fontSize = 18.sp,
                    color = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.weight(1.0f)
                )
                Text(
                    text = "${apply.minSalary}K--${apply.maxSalary}K",
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 20.sp,
                )
            }
            Row(horizontalArrangement = Arrangement.spacedBy(15.dp)) {
                Text(
                    text = apply.vacation,
                    color = MaterialTheme.colorScheme.secondary,
                    fontSize = 16.sp
                )
                Text(
                    text = "${apply.experience}年以上",
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
                Text(
                    text = apply.education,
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
                Text(
                    text = apply.address,
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
            }
            val list = apply.tag.split("-")
            LazyRow(
                horizontalArrangement = Arrangement.spacedBy(3.dp),
                content = {
                    items(list.size) {
                        Surface(
                            shape = RoundedCornerShape(15.dp),
                            color = MaterialTheme.colorScheme.surfaceTint
                        ) {
                            Text(
                                text = list[it],
                                modifier = Modifier
                                    .widthIn(min = 30.dp)
                                    .wrapContentSize(
                                        Alignment.Center
                                    )
                                    .padding(5.dp)
                            )
                        }
                    }
                })
        }
    }
}

